package easier.fast.base;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import easier.app.x.drawable.animator.AnimateProgressDrawable;
import easier.app.x.drawable.progress.load.DotPushDrawable;
import easier.fast.R;
import easier.window.dialog.BaseDialogFragment;
import easier.window.dialog.BaseLightDialog;

/**
 * {@link BaseActivity}中显示的加载中
 */
public class ProgressDialog extends BaseDialogFragment {

    public ProgressDialog() {

    }

    @NonNull
    @Override
    public Dialog onCreateDialog( @Nullable Bundle savedInstanceState ) {

        View dialogView = LayoutInflater.from( getContext() ).inflate( R.layout.dialog_progress, null );
        setDialogView( dialogView );
        initViewAction( dialogView );
        Dialog dialog = super.onCreateDialog( savedInstanceState );
        dialog.setCanceledOnTouchOutside( false );
        setCancelable( true );

        return dialog;
    }

    @Override
    protected Dialog createDialog( Context context, @Nullable Bundle savedInstanceState ) {

        return new BaseLightDialog( context ) {

            @Override
            public void onBackPressed() {

                super.onBackPressed();
                /* 此处处理返回按钮事件 */
                //FragmentActivity activity = getActivity();
                //if ( activity != null ) {
                //    activity.finish();
                //}
            }
        };
    }

    private void initViewAction( View dialogView ) {

        /* 设置动画效果 */
        View image1 = (View) dialogView.findViewById( R.id.image1 );
        DotPushDrawable progressDrawable = new DotPushDrawable();
        progressDrawable.setColor( Color.parseColor( "#EC6700" ) );
        AnimateProgressDrawable animateProgressDrawable = new AnimateProgressDrawable( progressDrawable );
        animateProgressDrawable.setCount( Integer.MAX_VALUE );
        animateProgressDrawable.setDuration( 1500 );
        image1.setBackground( animateProgressDrawable );
        image1.post( new Runnable() {
            @Override
            public void run() {

                animateProgressDrawable.start();
            }
        } );
    }
}
